package inl.resilienceManager.qmul.gemom;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYPointerAnnotation;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.EmptyBlock;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.CompositeTitle;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

import resilienceManager.qmul.gemom.Gbroker;
import resilienceManager.qmul.gemom.NSitem;

/** COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 * */

public class iniSolution extends ApplicationFrame
{
	  public iniSolution(String paramString)
	  {
	    super(paramString);
	   
	  }

	  private static CategoryDataset createDataset1(Gbroker[] glist)
	  {
	    String str1 = "Capability";
	    String str2 = "Threshold";
	    
	    
	    DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
	    
	    for(int i=0; i<glist.length; i++)
	    {
	    	String name="Gbroker-"+ glist[i].getID();
	    	double cap= glist[i].getCAP();
	    	double th= glist[i].getTh();
	    	localDefaultCategoryDataset.addValue(new Double(cap), str1, name);
		    localDefaultCategoryDataset.addValue(new Double(th), str2, name);
	    	
	    }
	    
	    
	    return localDefaultCategoryDataset;
	  }

	  private static CategoryDataset createDataset2(solution[] sl,Gbroker[] glist)
	  {
	    String str1 = "Size";

	    DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
	    
	    for(int i=0; i<glist.length; i++)
	    {
	    	String name="Item-"+ glist[i].getID();
	    	double s= sl[i].Size();

		    localDefaultCategoryDataset.addValue(new Double(s), str1, name);
	    	
	    }
	     return localDefaultCategoryDataset;
	  }

	  private static JFreeChart createChart(solution[] sl,Gbroker[] glist)
	  {
	    JFreeChart localJFreeChart = ChartFactory.createBarChart("Initialization Allocation", "Gbroker ID", "Capability Value", 
	    		createDataset1(glist), PlotOrientation.VERTICAL, false, true, false);
	    localJFreeChart.setBackgroundPaint(Color.white);
	    CategoryPlot localCategoryPlot = (CategoryPlot)localJFreeChart.getPlot();
	    localCategoryPlot.setBackgroundPaint(new Color(238, 238, 255));
	    localCategoryPlot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
	    CategoryDataset localCategoryDataset = createDataset2(sl,glist);
	    localCategoryPlot.setDataset(1, localCategoryDataset);
	    localCategoryPlot.mapDatasetToRangeAxis(1, 1);
	    CategoryAxis localCategoryAxis = localCategoryPlot.getDomainAxis();
	    localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
	    NumberAxis localNumberAxis = new NumberAxis("Item Size");
	    localCategoryPlot.setRangeAxis(1, localNumberAxis);
	    LineAndShapeRenderer localLineAndShapeRenderer = new LineAndShapeRenderer();
	    localLineAndShapeRenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
	    localCategoryPlot.setRenderer(1, localLineAndShapeRenderer);
	    localCategoryPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
	    LegendTitle localLegendTitle1 = new LegendTitle(localCategoryPlot.getRenderer(0));
	    localLegendTitle1.setMargin(new RectangleInsets(2.0D, 2.0D, 2.0D, 2.0D));
	    localLegendTitle1.setBorder(new BlockBorder());
	    LegendTitle localLegendTitle2 = new LegendTitle(localCategoryPlot.getRenderer(1));
	    localLegendTitle2.setMargin(new RectangleInsets(2.0D, 2.0D, 2.0D, 2.0D));
	    localLegendTitle2.setBorder(new BlockBorder());
	    BlockContainer localBlockContainer = new BlockContainer(new BorderArrangement());
	    localBlockContainer.add(localLegendTitle1, RectangleEdge.LEFT);
	    localBlockContainer.add(localLegendTitle2, RectangleEdge.RIGHT);
	    localBlockContainer.add(new EmptyBlock(2000.0D, 0.0D));
	    CompositeTitle localCompositeTitle = new CompositeTitle(localBlockContainer);
	    localCompositeTitle.setPosition(RectangleEdge.BOTTOM);
	    localJFreeChart.addSubtitle(localCompositeTitle);
	    
	    
	    return localJFreeChart;
	  }

	  public static JPanel createPanel(solution[] sl,Gbroker[] glist)
	  {
	    JFreeChart localJFreeChart = createChart(sl, glist);
	    return new ChartPanel(localJFreeChart);
	  }

	}
